import Vue from 'vue'
import Vuex from 'vuex'

import {
	setLocalStorage,
	getLocalStorage,
	getRemoveStorage
} from '@/utils'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		token: getLocalStorage('token'), // 用户token
		userInfo: getLocalStorage('userInfo'), // 用户信息
	},
	getters: {
		// 是否登录
		hasLogin(state) {
			return !!state.token;
		},
	},
	mutations: {
		// 存储token
		setToken(state, params) {
			setLocalStorage('token', params);
			state.token = params;
		},
		// 存储用户信息
		setUserInfo(state, params) {
			setLocalStorage('userInfo', params);
			state.userInfo = params;
		},
		// 清除用户信息
		removeUserInfo(state) {
			getRemoveStorage('userInfo');
			state.userInfo = {};
		},
	},
	actions: {}
})

export default store